#! /usr/bin/env python
#coding=utf-8

import players
import arenas

def getSubModules(module):
    # 获取子模块清单
    submodules = dir(module)
    for m in ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']:
        submodules.remove(m)
    # 排序
    submodules.sort()
    return submodules

def init_score_board(player_list, arena_list):
    score = {}
    l_player_list = player_list
    for l_playername in l_player_list:
        for r_playername in reversed(l_player_list):
            for arenaname in arena_list:
                matchname = "%s_%s_%s" % (arenaname, l_playername, r_playername)
                score[matchname] = {'arena':arenaname, 'left':0, 'left_name':l_playername, 'right':0, 'right_name':r_playername}

    return score

def fight(player_list, arena_list, rounds):
    score = init_score_board(player_list, arena_list)
    # print player_list, arena_list

    # 形成两两对决模式
    l_player_list = player_list
    for l_playername in l_player_list:
        for r_playername in reversed(l_player_list):
            # print l_playername, r_playername

            l_player = eval("players.%s" % l_playername)
            r_player = eval("players.%s" % r_playername)

            l_score = 0
            r_score = 0

            # 每对玩家多次对抗
            for round in range(0, rounds):
                # 每次对抗放到不同的 arena 得出结果
                i_arena = 0
                for arenaname in arena_list:
                    # 使用 i_arena 而不使用 arenaname 主要是为了避免 arena 环境信息
                    i_arena = i_arena + 1
                    l_card = l_player.play(i_arena)
                    r_card = r_player.play(i_arena)

                    arena = eval("arenas.%s" % arenaname)
                    matchname = "%s_%s_%s" % (arenaname, l_playername, r_playername)
                    # print arenaname, l_playername, r_playername

                    result = arena.compare(l_card, r_card)
                    l_result = result['left']
                    r_result = result['right']

                    # 把出牌结果完全反馈双方
                    my_card = l_card
                    my_result = l_result
                    o_card = r_card
                    o_result = r_result
                    l_player.feedback(i_arena, my_card, my_result, o_card, o_result)

                    my_card = r_card
                    my_result = r_result
                    o_card = l_card
                    o_result = l_result
                    r_player.feedback(i_arena, my_card, my_result, o_card, o_result)

                    score[matchname]['left'] = score[matchname]['left'] + l_result
                    score[matchname]['right'] = score[matchname]['right'] + r_result
                    # print score[matchname]



    return score

def showscore(scores):
    for score in scores:
        s = scores[score]
        if (s['left'] + s['right']) != 0:
            # 总得分为 0 代表没有比赛，总得分不是为 0
            print "Match: %s, %s(%s), %s(%s)" % (s['arena'], s['left_name'], s['left'], s['right_name'], s['right'])

def main():
    # 玩家清单
    player_list = getSubModules(players)
    # 竞技场清单
    arena_list = getSubModules(arenas)
    # 比赛轮数
    rounds = 1000

    scores = fight(player_list, arena_list, rounds)
    showscore(scores)

main()

